home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW Related / MPWTool / MPWTool.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-21  |  2.8 KB  |  99 lines  |  [TEXT/MPS ]

  1. //        PlayTool
  2. //
  3. //    This is an MPW tool which plays sounds. Given a list of sound resource names, it will play one
  4. //    sounds (for example, if they're in the System file). It will pick the sound to play at random. If
  5. //    a "-a" option is given, it will play all of the specified sounds in turn. If no sounds are specified,
  6. //    it will look in itself and use any sounds found there as the sound list to pick from at random, or
  7. //    to play all of (-a). It is, therefore, possible to make a copy of PlayTool by a different name,
  8. //    and then paste a set of sounds into it. When that new tool is invoked, it will play one of the sounds
  9. //    that were pasted into it. Thus, it can be used to make "build completed" and "build failed" tools,
  10. //    amoung others.
  11. //
  12. //    Things that could be added to make it even better:
  13. //        * Specifying a file to read the sound(s) from.
  14. //        * Volume control.
  15. //
  16. //    Initial coding 7/91 by Harry Chesley.
  17. //
  18. //    © Copyright 1991 by Apple Computer, Inc.
  19. //    All rights reserved.
  20.  
  21. #include <Types.h>
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include <strings.h>
  25. #include <stdio.h>
  26. #include <Events.h>
  27. #include <Sound.h>
  28. #include <Resources.h>
  29.  
  30. #define NIL 0
  31.  
  32. main(int argc, char *argv[])
  33. {
  34.     int allPlayOption;
  35.     int localOption;
  36.     int i;
  37.     int names;
  38.     int length;
  39.     Handle h;
  40.     Str255 theName;
  41.  
  42.     names = 0;
  43.     allPlayOption = false;
  44.     localOption = false;
  45.  
  46.     // Parse the parameter list:
  47.     for (i = 1; i < argc; i++) {
  48.         length = strlen(argv[i]);
  49.         if (*argv[i] != '-') argv[++names] = argv[i];
  50.         else if (tolower(*(argv[i]+1)) == 'a' && length == 2) allPlayOption = true;
  51.         else {
  52.             fprintf(stderr,"### %s - \"%s\" is not an option.\n", argv[0], argv[i]);
  53.             fprintf(stderr,"# Usage - %s [-a] [sound…].\n", argv[0]);
  54.             return 1;
  55.         }
  56.     }
  57.  
  58.     // If we weren't given any sound names, try introspection:
  59.     if (names == 0) {
  60.         names = Count1Resources('snd ');
  61.         localOption = true;
  62.     }
  63.  
  64.     // If there's still nothing to play, tell the user how to call us:
  65.     if (names == 0) {
  66.         fprintf(stderr, "# Usage - %s [-a] [sound…].\n", argv[0]);
  67.         return 1;
  68.  
  69.     // Otherwise, play, play, play:
  70.     } else {
  71.         // Decide between playing only one or everything in turn:
  72.         if (allPlayOption) i = 1;
  73.         else {
  74.             i = TickCount() % names + 1;
  75.             names = i;
  76.         };
  77.         // Cycle through all the sounds to be played:
  78.         for (; i <= names; i++) {
  79.             // Get the sound (either locally or in any file in the resource chain (probably the System file)):
  80.             if (localOption) h = Get1IndResource('snd ',i);
  81.             else {
  82.                 if (strlen(argv[i]) > 255) {
  83.                     fprintf(stderr,"Sound name too long (> 255)!\n");
  84.                     continue;
  85.                 };
  86.                 strcpy(&theName,argv[i]);
  87.                 c2pstr(theName);
  88.                 h = GetNamedResource('snd ',theName);
  89.             }
  90.             // Play it:
  91.             if (ResError() == 0) SndPlay(NIL,h,false);
  92.             else fprintf(stderr,"Could not find sound!\n");
  93.         }
  94.     }
  95.  
  96.     // Return success:
  97.     return 0;
  98. }
  99.